package training;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
class Point{
    public int x;
    public int y;
    public int level; // 表示从入口到x, y 走的步数

    public Point(int x, int y, int level) {
        this.x = x;
        this.y = y;
        this.level = level;
    }
}

//https://www.nowcoder.com/questionTerminal/6276dbbda7094978b0e9ebb183ba37b9
public class Day42_02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String[] strings = new String[10];
            for (int i = 0; i < strings.length; i++) {
                strings[i] = scanner.nextLine();
            }
            System.out.println(bfs(strings, 10, 10));
        }
    }

    private static int bfs(String[] strings, int m, int n) {
        // 方向数组 下右上左
        int[][] direct = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
        // 标记数组
        boolean[][] flags = new boolean[m][n];
        // 起点和终点
        Point enter = new Point(0, 1, 0);
        Point out = new Point(9, 8, 0);
        // 采用bfs走迷宫
        Deque<Point> queue = new LinkedList<>();
        queue.offer(enter);
        while (!queue.isEmpty()){
            Point cur = queue.poll();
            if(cur.x == out.x && cur.y == out.y){
                return cur.level;
            }
            flags[cur.x][cur.y] = true;
            for (int i = 0; i < 4; i++) {
                Point next = new Point(cur.x + direct[i][0],
                        cur.y + direct[i][1], cur.level + 1);
                if(next.x >= 0 && next.x < m && next.y >= 0 && next.y < n &&
                        strings[next.x].charAt(next.y) == '.' &&
                        !flags[next.x][next.y]){
                    queue.offer(next);
                }
            }
        }
        return 0;
    }
}
